import Vue from 'vue';
import App from './App';
import store from './store/index';
import popup from './common/api/popup.js';


// xs 添加的提示弹窗 封装 uni的提示弹窗
Vue.prototype.$popup = popup;

// 引入全局uView
import uView from "uview-ui";
Vue.use(uView);

/*===============================================================
=                           global user info                    =
===============================================================*/
global.token = '';
global.userInfo = null; // 用户信息
global.themeColor = '#F02523';


// 获取账户信息
try {
	const USER_TOKEN = uni.getStorageSync('USER_TOKEN');
	const USER_INFO = uni.getStorageSync('USER_INFO');
	global.token = USER_TOKEN || '';
	if (USER_INFO) global.userInfo = USER_INFO;
	global.userInfo = USER_INFO;
} catch (e) {}

/*======================================================================
=                    global attributes and methods                     =
======================================================================*/

import apiUrls from '@/common/api/url.js'
import {
	http
} from '@/common/api/index.js';
import {
	message,
	Router,
	Dayjs
} from '@/common/utils/index.js';
import {
	BASE_URL,
	IMG_BASE_URL,
	SYSTEM_CONFIG,
	PAGE_SIZE,
	VERSION_CODE,
	IS_DEV,
	needLoginPages,
	API_BASE_URL
} from '@/common/config.js';
import utils from '@/common/utils/utils';

const router = new Router({
	needLoginPages
});

Vue.config.productionTip = false;
Vue.prototype.$urouter = router;
Vue.prototype.$api = http;
Vue.prototype.$message = message;
Vue.prototype.$util = utils;
Vue.prototype.$store = store;

global.apiUrls = apiUrls;
global.imgBaseUrl = IMG_BASE_URL;
global.IS_DEV = IS_DEV;
global.PAGE_SIZE = PAGE_SIZE;
global.VERSION_CODE = VERSION_CODE;
global.SYSTEM_CONFIG = SYSTEM_CONFIG;
global.API_BASE_URL = API_BASE_URL;

// 弹出自定义Modal
global.customShowModalPopup = (options) => {
	global.customShowModelPopupOptions = options
	if (options.success) global.customShowModelPopupSucess = options.success
	// #ifdef APP-PLUS
	uni.navigateTo({
		url: '/pages/global-pop/modal/index'
	})
	// #endif
	// #ifndef APP-PLUS
	uni.showModal(options)
	// #endif
}

/*=================================================================
=                          global components                      =
=================================================================*/

import customPopModal from '@/pages/global-pop/modal/index-component.vue';

Vue.component('custom-pop-modal', customPopModal);

/*=================================================================
=                          global filters                         =
=================================================================*/


// 识别图片路径是否为全路径 若不是则进行拼接
Vue.filter('assembleImgSrc', src => {
	console.log(IMG_BASE_URL, src);
	src = src || ''
	if (src.indexOf('http://') != -1 || src.indexOf('https://') != -1) {
		return src
	}
	if (src.indexOf('/uploads/') != -1 || src.indexOf('/uploads/') != -1) {
		return BASE_URL + src
	} else {
		return IMG_BASE_URL + src
	}
});

// 日期格式转换
Vue.filter('formatDate', (date, format = 'YYYY-MM-DD hh:mm:ss') => {
	return Dayjs(date).format(format)
});


/*==================================================================
=                           global mixins                          =
==================================================================*/

Vue.mixin({
	data() {
		return {

		}
	},
	methods: {
		// 页面跳转
		handleJump(e) {
			let target = e.currentTarget || e.target,
				url = target.dataset.url || 'back',
				type = target.dataset.type;
			if (url == '' || url == '#') return;
			switch (type) {
				case 'SWITCH':
					router.switchTab(url);
					break;
				case 'REDIRECT':
					router.redirectTo(url);
					break;
				case 'RELAUNCH':
					router.reLaunch(url);
					break;
				case 'BACK':
					router.navigateBack();
					break;
				default:
					router.navigateTo(url);
					break;
			}
		}
	}
})

/*==================  End of global mixins  =====================*/


App.mpType = 'app';

const app = new Vue({
	...App,
	store
});

app.$mount();
